<?php
	require_once dirname(BASEPATH) . '/application/models/local/data/cafe/_init_model_cafe.php';	
	
	class mod_cafe extends _init_model_cafe{
		public $CAF_ID;
		public $CAF_CODE;
		public $CAF_NAME;
		public $CAF_TITLE;
		public $CAF_CREATE_DATE;
		public $CAF_ABOUT;
		public $CAF_FACEBOOK;
		public $CAF_WEBSITE;
		public $CAF_STATUS;
		public $CAF_IMG_L_ID;
		public $CAF_IMG_C_ID;
		public $CAF_LAST_ACTIVE;
		public $CAF_ULDATE_INFO;
		public $CAF_ULDATE_GAL;
		public $CAF_ULDATE_PDT;
		public $CAF_ULDATE_CON;
		public $CAF_FAV_NUM;
		public $MEM_ID;
		
		function mod_cafe(){
			parent::__construct();
						
			$this->tableName = "cafe";
			$this->CAF_STATUS = true;
			$this->PAK_ID = 1;			
		}				
		
		function check_cafe_state($cafe_name){
			$this->db->select('CAF_STATUS');
			$this->db->where('CAF_NAME', $cafe_name);
			$query = $this->db->get($this->tableName);
			
			if ($query->num_rows() > 0){
				if ($query->row(0)->CAF_STATUS)
					return true;
				else 
					return false;
			} else
				return false;				
		}
		function check_cafe_owner($cafe_name, $member_id){
			$this->db->where(array('CAF_NAME' => $cafe_name,
									'MEM_ID' => $member_id));
			$count = $this->db->count_all_results($this->tableName);
			
			if ($count > 0)
				return true;
			else
				return false;
		}	
		function check_cafe_exist($cafe_name){
			$sqlcmd = 'SELECT COUNT(CAF_ID) AS T_COUNT';
			$sqlcmd .= ' FROM ' . $this->tableName;
			$sqlcmd .= ' WHERE CAF_NAME = "' . $cafe_name . '"';
			
			$query = $this->db->query($sqlcmd);
			if ($query->num_rows() > 0){
				if ($query->row()->T_COUNT > 0)
					return true;
				else
					return false;
			}
			else
				return false;
		}		
		function check_cafe_exist_by_id($cafe_id){
			$sqlcmd = 'SELECT COUNT(CAF_ID) AS T_COUNT';
			$sqlcmd .= ' FROM ' . $this->tableName;
			$sqlcmd .= ' WHERE CAF_ID = ' . $cafe_id;
			
			$query = $this->db->query($sqlcmd);
			if ($query->num_rows() > 0)
			{
				if ($query->row()->T_COUNT > 0)
					return true;
				else
					return false;
			}
			else
				return false;
		}
		function check_available_name($cafe_name){
			$this->db->where('CAF_NAME', $cafe_name);
			$count = $this->db->count_all_results($this->tableName);
				
			if ($count > 0)
				return false;
			else
				return true;
		}
		function check_is_favorited_cafe($cafe_id, $member_id){
			$this->load->model('local/data/mod_member');
			$result = $this->mod_member->get_member($member_id, array('MEM_CAF_FAVORITE'));
			
			if (in_array($cafe_id, explode(',', $result['MEM_CAF_FAVORITE'])))
				return true;
			else
				return false;
		}
				
		function get_single_cafe($cafe_id){			
			$this->db->where('cafe.CAF_ID', $cafe_id);
			$this->db->join('cafe_img_logo', 'cafe_img_logo.CAF_IMG_L_ID = cafe.CAF_IMG_L_ID', 'left');
			$this->db->join('cafe_img_cover', 'cafe_img_cover.CAF_IMG_C_ID = cafe.CAF_IMG_C_ID', 'left');
			$query = $this->db->get($this->tableName);
			
			if ($query->num_rows() > 0){
				$this->load->library('define/directory_path');				
				$return = $query->row_array(0);
				
				if (!is_null($return['CAF_IMG_L_ID']))
					$return['CAF_IMG_L_FILENAME'] = $this->directory_path->img_logo_path . $return['CAF_IMG_L_FILENAME'];
				else
					$return['CAF_IMG_L_FILENAME'] = $this->directory_path->img_no_logo_path;
				
				if (!is_null($return['CAF_IMG_C_ID']))
					$return['CAF_IMG_C_FILENAME'] = $this->directory_path->img_cover_path . $return['CAF_IMG_C_FILENAME'];
				else
					$return['CAF_IMG_C_FILENAME'] = $this->directory_path->img_no_cover_path;
				
				return $return;
			}
			else 
				return null;
		}
		function get_single_cafe_by_name($cafe_name, $fields=null){
			
			if (!is_null($fields) && is_array($fields))
				$this->db->select($fields);
			
			$this->db->where('CAF_NAME', strtolower($cafe_name));
			$this->db->join('cafe_img_logo', 'cafe_img_logo.CAF_IMG_L_ID = cafe.CAF_IMG_L_ID', 'left');
			$this->db->join('cafe_img_cover', 'cafe_img_cover.CAF_IMG_C_ID = cafe.CAF_IMG_C_ID', 'left');
			$query = $this->db->get($this->tableName);
			
			if ($query->num_rows() > 0)	{
				$this->load->library('define/directory_path');				
				$return = $query->row_array(0);
				
				if (!empty($return['CAF_IMG_L_FILENAME']) && isset($return['CAF_IMG_L_FILENAME']))
					$return['CAF_IMG_L_FILENAME'] = $this->directory_path->img_logo_path . $return['CAF_IMG_L_FILENAME'];
				else
					$return['CAF_IMG_L_FILENAME'] = $this->directory_path->img_no_logo_path;
				
				if (!empty($return['CAF_IMG_C_FILENAME']) && isset($return['CAF_IMG_C_FILENAME']))
					$return['CAF_IMG_C_FILENAME'] = $this->directory_path->img_cover_path . $return['CAF_IMG_C_FILENAME'];
				else
					$return['CAF_IMG_C_FILENAME'] = $this->directory_path->img_no_cover_path;
				
				return $return;
			}			
			else
				return null;
		}
		function get_shuffle_cafe($count=null){
			
			$this->db->select(array('cafe.CAF_ID','cafe.CAF_NAME','cafe.CAF_TITLE','cafe.MEM_ID','cafe.CAF_CREATE_DATE',
									'cafe_img_logo.CAF_IMG_L_FILENAME'));
			$this->db->join('cafe_img_logo', 'cafe_img_logo.CAF_IMG_L_ID = cafe.CAF_IMG_L_ID', 'left');
			$this->db->where('CAF_STATUS', 1);
			$this->db->order_by('CAF_ID', 'RANDOM');
			
			if (!is_null($count))
				$this->db->limit($count);
			
			$query = $this->db->get($this->tableName);
			$return = $query->result_array();
			
			if (count($return) > 0){
				$this->load->library('define/directory_path');
								
				for ($i=0; $i<count($return); $i++){
					if (empty($return[$i]['CAF_IMG_L_FILENAME']))					
						$return[$i]['CAF_IMG_L_FILENAME'] = $this->directory_path->img_no_logo_path;
					else				
						$return[$i]['CAF_IMG_L_FILENAME'] = $this->directory_path->img_logo_path . $return[$i]['CAF_IMG_L_FILENAME'];
				}
			}
			
			return $return;
			
		}
		function get_cafe_title_by_name($cafe_name){
			$this->db->select('CAF_TITLE');
			$this->db->where('CAF_NAME', $cafe_name);
			$query = $this->db->get($this->tableName);
			
			if ($query->num_rows() > 0)
				return $query->row(0)->CAF_TITLE;
			else
				return null;
		}		
		function get_cafe_of_member($member_id){
			
			$this->db->select(array('cafe.CAF_ID','cafe.CAF_CODE','cafe.CAF_NAME','cafe.CAF_TITLE','cafe.MEM_ID','cafe.CAF_CREATE_DATE','cafe.CAF_STATUS',
									'cafe_img_logo.CAF_IMG_L_FILENAME'));
			$this->db->where(array('MEM_ID' => $member_id));
			$this->db->join('cafe_img_logo', 'cafe_img_logo.CAF_IMG_L_ID = cafe.CAF_IMG_L_ID', 'left');
			$this->db->order_by('cafe.CAF_ID', 'DESC');			
			
			$query = $this->db->get($this->tableName);
			
			$return = $query->result_array();			
			if ($query->num_rows() > 0){
				for ($i=0; $i<count($return); $i++){
					
					// check if logo has added
					$this->load->library('define/directory_path');
					if (empty($return[$i]['CAF_IMG_L_FILENAME'])){												
						$return[$i]['CAF_IMG_L_FILENAME'] = $this->directory_path->img_no_logo_path;
					} else
						$return[$i]['CAF_IMG_L_FILENAME'] = $this->directory_path->img_logo_path . $return[$i]['CAF_IMG_L_FILENAME'];
					
					$this->load->model('local/data/cafe/mod_cafe_expire');
					$curr_package = $this->mod_cafe_expire->get_current_package_state($return[$i]['CAF_ID']);
					
					// set current package information
					if (!is_null($curr_package) && ($curr_package['PAK_ID'] != 1)) {
						$return[$i]['DAY_REMAIN'] = $curr_package['DAY_REMAIN'];
						$return[$i]['CURRENT_PACKAGE_ID'] = $curr_package['PAK_ID'];
						$return[$i]['CURRENT_PACKAGE_NAME'] = $curr_package['PAK_NAME'];
					} else {
						// set free package
						$this->load->model('local/data/mod_package');
						$free_package = $this->mod_package->get_package(1);
						
						$return[$i]['DAY_REMAIN'] = '&#8734;';
						$return[$i]['CURRENT_PACKAGE_ID'] = $curr_package['PAK_ID'];
						$return[$i]['CURRENT_PACKAGE_NAME'] = $free_package['PAK_NAME'];
					}
				}
			}		
			return $return;	
		}
		function get_cafe_list($display_length=null, $start_index=null, $field=null){

			if (!is_null($field))
				$this->db->select($field);
			else
				$this->db->select(array('cafe.*',
									'cafe_img_logo.CAF_IMG_L_FILENAME', 'cafe_img_cover.CAF_IMG_C_FILENAME'));
						
			$this->db->join('cafe_img_logo', 'cafe_img_logo.CAF_IMG_L_ID = cafe.CAF_IMG_L_ID', 'left');
			$this->db->join('cafe_img_cover', 'cafe_img_cover.CAF_IMG_C_ID = cafe.CAF_IMG_C_ID', 'left');
			$this->db->join('member', 'member.MEM_ID = cafe.MEM_ID', 'left');
			$this->db->order_by('cafe.CAF_ID', 'DESC');
			
			if (!is_null($start_index) && !is_null($display_length)){
				if ($start_index < 0)
					$start_index = 0;
				$query = $this->db->get($this->tableName, $display_length, $start_index);
			} else
				$query = $this->db->get($this->tableName);
			
			$return = $query->result_array();
			if ($query->num_rows() > 0){
				
				$this->load->library('define/directory_path');
				
				for ($i=0; $i<count($return); $i++){
					if (!empty($return[$i]['CAF_IMG_L_FILENAME']) && isset($return[$i]['CAF_IMG_L_FILENAME']))
						$return[$i]['CAF_IMG_L_FILENAME'] = $this->directory_path->img_logo_path . $return[$i]['CAF_IMG_L_FILENAME'];
					else
						$return[$i]['CAF_IMG_L_FILENAME'] = $this->directory_path->img_no_logo_path;
					
					if (!empty($return[$i]['CAF_IMG_C_FILENAME']) && isset($return[$i]['CAF_IMG_L_FILENAME']))
						$return[$i]['CAF_IMG_C_FILENAME'] = $this->directory_path->img_cover_path . $return[$i]['CAF_IMG_C_FILENAME'];
					else
						$return[$i]['CAF_IMG_C_FILENAME'] = $this->directory_path->img_no_cover_path;
				}
			}
			
			return $return;	
		}
		function search_cafe($keyword=null, $search_mode=0,  $types=array(), $province_id=null, $start_index=null, $display_length=null){
			
			$this->db->select(array('cafe.*',
					'cafe_img_logo.CAF_IMG_L_FILENAME', 'cafe_img_cover.CAF_IMG_C_FILENAME'));
			
			$this->db->join('cafe_img_logo', 'cafe_img_logo.CAF_IMG_L_ID = cafe.CAF_IMG_L_ID', 'left');
			$this->db->join('cafe_img_cover', 'cafe_img_cover.CAF_IMG_C_ID = cafe.CAF_IMG_C_ID', 'left');

			// query keyword
			if (!is_null($keyword) && !empty($keyword)){
				switch ($search_mode){
					case 0: default:
						$this->db->like('cafe.CAF_TITLE', $keyword, 'both');
						break;
					case 1:
						$this->db->like('cafe.CAF_TITLE', $keyword, 'after');
						break;
					case 2:
						$this->db->like('cafe.CAF_TITLE', $keyword, 'before');
						break;
				}
			}
			
			// query type
			if (!is_null($types) && is_array($types)){
				$sqlcmd = 'SELECT CAF_ID FROM cafe_type_list';
				for ($i=0; $i<count($types); $i++){
					if ($i > 0)
						$sqlcmd .= ' OR FIND_IN_SET(' . $types[$i] .  ', CAF_T_L_LIST)';
					else
						$sqlcmd .= ' WHERE FIND_IN_SET(' . $types[$i] . ', CAF_T_L_LIST)';
				}				
				$this->db->or_where('cafe.CAF_ID IN (' . $sqlcmd . ')');
			}
			
			// query province
			if (!is_null($province_id) && $province_id != -1){
				$sqlcmd = 'SELECT CAF_ID FROM cafe_contact WHERE PROVINCE_ID = ' . $province_id;
				$this->db->or_where('cafe.CAF_ID IN (' . $sqlcmd . ')');
			}		
			
			$this->db->order_by('CAF_LAST_ACTIVE', 'DESC');
			$db_cache = clone $this->db;
						
			if (!is_null($start_index) && !is_null($display_length)){
				if ($start_index < 0)
					$start_index = 0;
				$query = $this->db->get($this->tableName, $display_length, $start_index);				
			} else
				$query = $this->db->get($this->tableName);			
				
			$return = $query->result_array();			
			if ($query->num_rows() > 0){
				$this->load->library('define/directory_path');
			
				for ($i=0; $i<count($return); $i++){
					if (!empty($return[$i]['CAF_IMG_L_FILENAME']) && isset($return[$i]['CAF_IMG_L_FILENAME']))
						$return[$i]['CAF_IMG_L_FILENAME'] = $this->directory_path->img_logo_path . $return[$i]['CAF_IMG_L_FILENAME'];
					else
						$return[$i]['CAF_IMG_L_FILENAME'] = $this->directory_path->img_no_logo_path;
						
					if (!empty($return[$i]['CAF_IMG_C_FILENAME']) && isset($return[$i]['CAF_IMG_L_FILENAME']))
						$return[$i]['CAF_IMG_C_FILENAME'] = $this->directory_path->img_cover_path . $return[$i]['CAF_IMG_C_FILENAME'];
					else
						$return[$i]['CAF_IMG_C_FILENAME'] = $this->directory_path->img_no_cover_path;
				}
			}			
			
			$this->load->model('local/data/cafe/mod_cafe_img_cover');
			for ($i=0; $i<count($return); $i++){
				$return[$i]['CAF_IMG_C_LIST'] = $this->mod_cafe_img_cover->get_cover_list($return[$i]['CAF_ID'], 3);
			}			
			
			$this->load->model('local/data/cafe/mod_cafe_contact');
			for ($i=0; $i<count($return); $i++){
				$return[$i]['PROVINCE_LIST'] = $this->mod_cafe_contact->get_province_list_of_cafe($return[$i]['CAF_ID']);
			}		
			
			return array('result' => $return,
							'row_count' => $db_cache->count_all_results($this->tableName));
		}
				
		function get_cafe_current_logo($cafe_name){
			$this->db->select(array('cafe.CAF_IMG_L_ID', 'cafe_img_logo.CAF_IMG_L_FILENAME'));
			$this->db->join('cafe_img_logo', 'cafe.CAF_IMG_L_ID = cafe_img_logo.CAF_IMG_L_ID');
			$this->db->where('cafe.CAF_NAME', $cafe_name);
			$query = $this->db->get($this->tableName);			
			
			$this->load->library('define/directory_path');			
			if ($query->num_rows() > 0){
				$return = $query->row_array(0);
				
				$return['CAF_IMG_L_FILENAME'] = $this->directory_path->img_logo_path . $return['CAF_IMG_L_FILENAME'];
				return $return;
			}
			else
				return array('CAF_IMG_L_ID' => null,
								'CAF_IMG_L_FILENAME' => $this->directory_path->img_no_logo_path);
		}		
		function get_cafe_current_cover($cafe_name){
			$this->db->select(array('cafe.CAF_IMG_C_ID', 'cafe_img_cover.CAF_IMG_C_FILENAME'));
			$this->db->join('cafe_img_cover', 'cafe.CAF_IMG_C_ID = cafe_img_cover.CAF_IMG_C_ID');
			$this->db->where('cafe.CAF_NAME', $cafe_name);
			$query = $this->db->get($this->tableName);			
			
			$this->load->library('define/directory_path');
			if ($query->num_rows() > 0){
				$return = $query->row_array(0);
				
				$return['CAF_IMG_C_FILENAME'] = $this->directory_path->img_cover_path . $return['CAF_IMG_C_FILENAME'];
				return $return;
			}
			else
				return array('CAF_IMG_C_ID' => null,
								'CAF_IMG_C_FILENAME' => $this->directory_path->img_no_cover_path);
		}
		
		function get_cafe_name($cafe_id){
			$this->db->select('CAF_NAME');
			$this->db->where('CAF_ID', $cafe_id);
			$query = $this->db->get($this->tableName);
			
			if ($query->num_rows() > 0)
				return $query->row(0)->CAF_NAME;
			else
				return null;
		}
		function get_cafe_about($cafe_name) {
			$this->db->select(array('CAF_ABOUT'));
			$this->db->where('CAF_NAME', $cafe_name);
			$query = $this->db->get($this->tableName);
			
			if ($query->num_rows() > 0)
				return $query->row(0)->CAF_ABOUT;
			else
				return null;
		}
		function get_cafe_id_from_name($cafe_name){
			$this->db->select('CAF_ID');
			$this->db->where('CAF_NAME', $cafe_name);
			$query = $this->db->get($this->tableName);
			if ($query->num_rows() > 0)
				return $query->row(0)->CAF_ID;
			else 
				return null;
		}
		function get_cafe_id_from_code($cafe_code){
			$this->db->select('CAF_ID');
			$this->db->where('CAF_CODE', $cafe_code);
			$query = $this->db->get($this->tableName);
			if ($query->num_rows() > 0)
				return $query->row(0)->CAF_ID;
			else 
				return null;
		}
		function get_cafe_id_from_title($cafe_title){
			$this->db->select('CAF_ID');
			$this->db->where('CAF_TITLE', $cafe_title);
			$query = $this->db->get($this->tableName);
			if ($query->num_rows() > 0)
				return $query->row(0)->CAF_ID;
			else
				return null;
		}
		function get_count($member_id=null){
			if (!is_null($member_id))
				$this->db->where('MEM_ID', $member_id);
			return parent::get_count();
		}
		
		function is_available_cafe($cafe_name){
			$this->db->select('CAF_STATUS');
			$this->db->where('CAF_NAME', $cafe_name);
			$query = $this->db->get($this->tableName);
			
			if ($query->num_rows() > 0){
				if ($query->row(0)->CAF_STATUS)
					return 1; // cafe is available
				else
					return 0; // cafe is disabled
			} else 
				return -1; // cafe not found
		}
		
		function set_last_active($cafe_name){
			$this->load->library('custom_datetime');
			
			$data = array('CAF_LAST_ACTIVE' => $this->custom_datetime->current_datetime());
			$this->db->where(array('CAF_NAME' => $cafe_name));			
			return $this->db->update($this->tableName, $data);
		}
		function set_selected_cover($cover_id, $cafe_name){
			$this->db->where('CAF_NAME', $cafe_name);
			return $this->db->update($this->tableName, array('CAF_IMG_C_ID' => $cover_id));
		}
		function set_selected_logo($logo_id, $cafe_name){
			$this->db->where('CAF_NAME', $cafe_name);
			return $this->db->update($this->tableName, array('CAF_IMG_L_ID' => $logo_id));
		}
		
		function save_cafe_about($content, $cafe_id){
			$this->db->where('CAF_ID', $cafe_id);
			return $this->db->update($this->tableName, array('CAF_ABOUT' => $content));
		}
		function change_cafe_status($cafe_id, $status){
			$this->db->where('CAF_ID', $cafe_id);
			return $this->db->update($this->tableName, array('CAF_STATUS' => $status));
		}
		function generate_next_cafe_code(){
			$this->load->library('define/general_define');
			$char_length = $this->general_define->cafe_id_char_length;
			
			$sqlcmd = 'SELECT MAX(RIGHT(CAF_CODE, ' . $char_length . ')) AS MAX_CAF_CODE';
			$sqlcmd .= ' FROM ' . $this->tableName;
			$query = $this->db->query($sqlcmd);
			
			$max_id = str_replace('0', '', $query->row(0)->MAX_CAF_CODE);
			$new_code = $this->general_define->cafe_prefix . date('y') . date('m') . '-';
						
			if ($max_id == '') 
				$loop_length = $char_length;				 
			else				
				$loop_length = ($char_length+1) - strlen($max_id);
			
			for($i=0; $i<$loop_length; $i++)
				$new_code .= '0';		
			
			return $new_code . ($max_id +1);		
		}

		function increase_favorite($cafe_id){
			$this->db->select('CAF_FAV_NUM');
			$this->db->where('CAF_ID', $cafe_id);
			$query = $this->db->get($this->tableName);
			
			if ($query->num_rows() > 0){
				$num = $query->row(0)->CAF_FAV_NUM + 1;
				
				$this->db->flush_cache();
					
				$this->db->where('CAF_ID', $cafe_id);
				return $this->db->update($this->tableName, array('CAF_FAV_NUM' => $num));
			} else
				return false;						
		}
		function decrease_favorite($cafe_id){
			$this->db->select('CAF_FAV_NUM');
			$this->db->where('CAF_ID', $cafe_id);
			$query = $this->db->get($this->tableName);
				
			if ($query->num_rows() > 0){
				$num = $query->row(0)->CAF_FAV_NUM - 1;
				$this->db->flush_cache();
				
				$this->db->where('CAF_ID', $cafe_id);
				return $this->db->update($this->tableName, array('CAF_FAV_NUM' => $num));
			}else
				return false;
			
		}
				
		function add(){
			$this->load->library('custom_datetime');
			$this->CAF_CREATE_DATE = $this->custom_datetime->current_date();
			
			$data = array('CAF_CODE' => $this->generate_next_cafe_code(),
							'CAF_NAME' => $this->CAF_NAME,
							'CAF_TITLE' => $this->CAF_TITLE,
							'CAF_CREATE_DATE' => $this->CAF_CREATE_DATE,
							'CAF_ABOUT' => $this->CAF_ABOUT,
							'CAF_FACEBOOK' => $this->CAF_FACEBOOK,
							'CAF_WEBSITE' => $this->CAF_WEBSITE,
							'CAF_STATUS' => $this->CAF_STATUS,
							'MEM_ID' => $this->MEM_ID);
			return $this->db->insert($this->tableName, $data);
		}
		function edit($cafe_id){
			$data = array();
			if (!empty($this->CAF_CODE))
				$data['CAF_CODE'] = $this->CAF_CODE;
			if (!empty($this->CAF_NAME))
				$data['CAF_NAME'] = $this->CAF_NAME;
			if (!empty($this->CAF_TITLE))
				$data['CAF_TITLE'] = $this->CAF_TITLE;
			if (!empty($this->CAF_ABOUT))
				$data['CAF_ABOUT'] = $this->CAF_ABOUT;
			if (!empty($this->CAF_FACEBOOK))
				$data['CAF_FACEBOOK'] = $this->CAF_FACEBOOK;
			if (!empty($this->CAF_WEBSITE))
				$data['CAF_WEBSITE'] = $this->CAF_WEBSITE;
			if (!empty($this->CAF_STATUS))
				$data['CAF_STATUS'] = $this->CAF_STATUS;
			if (!empty($this->MEM_ID))
				$data['MEM_ID'] = $this->MEM_ID;				
			if (!empty($this->CAF_IMG_L_ID))
				$data['CAF_IMG_L_ID'] = $this->CAF_IMG_L_ID;
			if (!empty($this->CAF_IMG_C_ID))
				$data['CAF_IMG_C_ID'] = $this->CAF_IMG_C_ID;				
			if (!empty($this->CAF_LAST_ACTIVE))
				$data['CAF_LAST_ACTIVE'] = $this->CAF_LAST_ACTIVE;
			if (!empty($this->CAF_ULDATE_INFO))
				$data['CAF_ULDATE_INFO'] = $this->CAF_ULDATE_INFO;
			if (!empty($this->CAF_ULDATE_GAL))
				$data['CAF_ULDATE_GAL'] = $this->CAF_ULDATE_GAL;
			if (!empty($this->CAF_ULDATE_PDT))
				$data['CAF_ULDATE_PDT'] = $this->CAF_ULDATE_PDT;
			if (!empty($this->CAF_ULDATE_CON))
				$data['CAF_ULDATE_CON'] = $this->CAF_ULDATE_CON;
			if (!empty($this->CAF_FAV_NUM))
				$data['CAF_FAV_NUM'] = $this->CAF_FAV_NUM;
				
			if (count($data) > 0){
				$this->db->where('CAF_ID', $cafe_id);	
				return $this->db->update($this->tableName, $data);
			}
			else
				return true;
		}
		function remove($cafe_id){
			$this->load->model('local/data/cafe/mod_cafe_contact');		
			$this->load->model('local/data/cafe/mod_cafe_expire');
			$this->load->model('local/data/cafe/mod_cafe_img_cover');
			$this->load->model('local/data/cafe/mod_cafe_img_logo');
			$this->load->model('local/data/cafe/mod_cafe_img_gallery');
			$this->load->model('local/data/cafe/mod_cafe_product');
			$this->load->model('local/data/cafe/mod_cafe_type_list');

			$this->mod_cafe_contact->remove_by_cafe($cafe_id);
			$this->mod_cafe_expire->remove_by_cafe($cafe_id);
			$this->mod_cafe_img_cover->remove_by_cafe($cafe_id);
			$this->mod_cafe_img_logo->remove_by_cafe($cafe_id);
			$this->mod_cafe_img_gallery->remove_by_cafe($cafe_id);
			$this->mod_cafe_product->remove_by_cafe($cafe_id);
			$this->mod_cafe_type_list->remove_by_cafe($cafe_id);
			
			$name = $this->get_cafe_name($cafe_id);
			
			$this->db->where('CAF_ID', $cafe_id);
			if ($this->db->delete($this->tableName)){				
				unlink(dirname(BASEPATH) . '/application/controllers/' . $name . '.php');
				return true;
			} else
				return false;		
		}
		function remove_by_code($cafe_code){
			$id = $this->get_cafe_id_from_code($cafe_code);
			if (!is_null($id))
				return $this->remove($id);
			else
				return false;			
		}

	}
?>